package br.unirio.analise_dna.action;

import org.apache.commons.mail.EmailException;
import com.opensymphony.xwork2.ActionSupport;
import br.unirio.analise_dna.entity.Email;

public class EmailService extends ActionSupport  {
	/**
	 * 
	 */
	private String first_name;
	private String last_name;
	private String email;
	private String subject;
	private String message;
		
	public void setFirst_name(String first_name)
	{
		this.first_name = first_name;
	}
	
	public void setLast_name(String last_name)
	{
		this.last_name = last_name;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}

	public void setSubject(String subject)
	{
		this.subject = subject;
	}
	
	public void setMessage(String message)
	{
		this.message = message;
	}
		
	private static final long serialVersionUID = 1L;

	public String enviarEmail (){
		
					
		Email sendEmail = new Email();
	
		sendEmail.setSubject(subject);
		sendEmail.setEmail(email);
		sendEmail.setFirst_name(first_name);
		sendEmail.setLast_name(last_name);
		sendEmail.setMessage(message);
		
		
		if (first_name.length() == 0)
			addFieldError("first_name", getText("error.first_name.required"));
		
		if (last_name.length() == 0)
			addFieldError("last_name", getText("error.last_name.required"));
		
		if (email.length() == 0)
			addFieldError("email", getText("error.email.required"));
		
		if (message.length() == 0)
			addFieldError("message", getText("error.message.required"));
		
		if (subject.length() == 0)
			addFieldError("subject", getText("error.subject.required"));
		
		
		if (hasErrors())
			return INPUT;
		
		
		try {
			sendEmail.enviarEmail();
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			addFieldError("first_name", getText("error.send.email"));
			return ERROR;
		}
		return SUCCESS;
	}
}